#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"

#include <FL/Fl.H>
#include <FL/Fl_Gl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/fl_file_chooser.H>

#include "BList.h"
#include "BVector.h"
#include "quaternion.h"
#include "symbol.h"
#include "sort.h"
#include "ontology.h"
#include "featureterm.h"
#include "FTKBase.h"

#include "myglutaux.h"
#include "CMC.h"
#include "3dobject.h"
#include "AWLocalViewport.h"

#include "AWMessage.h"
#include "AWEntity.h"
#include "AWScenario.h"
#include "AWBody.h"
#include "AWSensor.h"
#include "AWBrain.h"
#include "AWAgent.h"
#include "AWWorld.h"

#include "awbwindow.h"
#include "awwindow.h"

#include "loader.h"


extern AW_World *World;
extern AWWindow *ww;
extern bool active;
extern bool running;
extern float world_time;
extern float TIME_INC,MAX_TIME_INC;
extern float TIME_RATE;

void load_cb(Fl_Widget *,void *)
{
	FILE *fp;
	char *filename;

	filename=fl_file_chooser("Load world","*.wld","*.wld");

	if (filename!=0) {
		world_time=0;

		fp=fopen(filename,"r");
		if (World!=0) delete World;
		World=(AW_World *)ClassLoader(fp,0,0);
		world_time=World->GetActTime();
		MAX_TIME_INC=World->GetMaxTimeInc();
		TIME_RATE=World->GetTimeRate();
		fclose(fp);
		if (!active) World->pause();
			    else World->unpause();
	} /* if */ 

	ww->redraw();
} /* load_cb */ 


void save_cb(Fl_Widget *,void *)
{
	FILE *fp;
	char *filename;

	filename=fl_file_chooser("Save world","*.wld","tmpworld.wld");

	if (filename!=0) {
		fp=fopen(filename,"w");
		World->save(fp);
		fclose(fp);
	} /* if */ 
} /* save_cb */ 


void start_cb(Fl_Widget *,void *)
{
	World->unpause();
	active=true;
} /* start_cb */ 


void stop_cb(Fl_Widget *,void *)
{
	World->pause();
	active=false;
} /* stop_cb */ 


void quit_cb(Fl_Widget *, void *)
{
	running=false;
} /* quit_cb */ 


void x1_cb(Fl_Widget *, void *)
{
	TIME_RATE=World->GetTimeRate();
} /* x1_cb */ 


void x2_cb(Fl_Widget *, void *)
{
	TIME_RATE=World->GetTimeRate()*2;
} /* x2_cb */ 


void x4_cb(Fl_Widget *, void *)
{
	TIME_RATE=World->GetTimeRate()*4;
} /* x4_cb */ 


void x8_cb(Fl_Widget *, void *)
{
	TIME_RATE=World->GetTimeRate()*8;
} /* x8_cb */ 
